import apiUrl from '@/common/config.js'
// console.log(apiUrl)
const request = (url, method = 'GET', data = {}, showLoading = true) => {

	const token = uni.getStorageSync('token'); // 从本地存储获取 token
	// console.log(token, 'token')
	const headers = {
		// 'Authorization': token ? token = `${token}` : '', // 设置请求头中的 token
		'token': token, // 设置请求头中的 token
		'Content-Type': 'application/json',
	};
	if (showLoading) {
		uni.showLoading({
			title: '加载中...'
		});
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url: apiUrl + url,
			method: method,
			data: data,
			header: headers,
			success: (res) => {
				uni.hideLoading();
				if (res.statusCode === 200) {
					resolve(res.data);
				} else {
					reject(res);
				}
			},
			fail: (err) => {
				uni.hideLoading();
				reject(err);
			}
		});
	});
};

export default request;